package main

import (
	"bytes"
	"fmt"
	"log"
	"os"
	"os/exec"
	"time"
)

//运行命令的
var (
	cmd *exec.Cmd
)

func Kill() {
	if cmd != nil && cmd.Process != nil {
		err := cmd.Process.Kill()
		if err != nil {
			log.Fatal(err)
		}
	}
}

func Start(out *bytes.Buffer, started chan bool) {
	cmdString1 := "/usr/lib/golang/bin/go"
	cmdString2 := "run"
	cmdString3 := "/home/zxx/src/go/src/git.oschina.net/zd04/golang-learn/20170103/fs_w2.go"
	cmd := exec.Command(cmdString1, cmdString2, cmdString3)

	cmd.Stdout = out
	//cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	//		err := cmd.Run()
	//		if err != nil {
	//			log.Fatal(err)
	//		}

	go cmd.Run()

	//started <- true
}

func main() {

	var started = make(chan bool)
	var out bytes.Buffer

	go Start(&out, started)

	<-time.After(time.Second * 3)
	Kill()

	fmt.Println(<-started)

	fmt.Printf("in all caps: %q\n", out.String())
}
